﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BitAuto.EP.CRMLogSystem.Utils;
using CRMSimple.LogService.DataProducter.Abstractions;
using CRMSimple.LogService.DataProtocol.Abstractions;
using BitAuto.EP.CRMLogSystem.Entity;
using CRMSimple.LogService.DataProducter;
using BitAuto.EP.FuncPMonitor.Aops;

namespace CRMSimple.LogServices.DataProducter
{
    public class RMQDataProducter : DataProducterRoot
    {
        /// <summary>
        /// 终止接收
        /// </summary>
        public override void StopProduceData()
        {
            try
            {
                //RedLockBuilder.GetInit().Dispose();
                RabbitMQSync.Stop();
            }
            catch (Exception ex)
            {
                ////记录错误日志
                //MaintainRelations.GrappleLog.InfoFormat("客户版商机客户关系维护服务，终止RabbitMQ推送异常，异常信息：{0}",
                //    ex.Message);
                ////发送邮件提醒
                //string title = string.Format("客户版商机客户关系维护服务，终止RabbitMQ推送异常。");
                //string msg = string.Format("异常信息：{0}", ex.Message);
                //ErrorAlertMailHelper.SendAlertMail(title, msg);
            }
        }
        /// <summary>
        /// 生产数据
        /// </summary>
         [FuncPMonitor()]
        public override void StartProduceData(Action<DataParam> action)
        {
            try
            {
                RabbitMQSync.ReceiveData((byte[] msg, DateTime upDate) =>
                {
                    action(new DataParam(Encoding.UTF8.GetString(msg), upDate));
                });
            }
            catch (Exception ex)
            {
                ////记录错误日志
                //MaintainRelations.GrappleLog.InfoFormat("客户版商机中心RabbitMQ推送异常，异常信息：{0}",
                //    ex.Message);
                ////发送邮件提醒
                //string title = string.Format("客户版商机中心RabbitMQ推送异常。");
                //string msg = string.Format("异常信息：{0}", ex.Message);
                //ErrorAlertMailHelper.SendAlertMail(title, msg);
            }
            int i = 0;
            while (i < EConvert.ToInt(AppConfig.GetAppSetting("CreateData")))
            {
                i++;
                action(new DataParam(Encoding.UTF8.GetString(new ProduceDataForMQ() { LogContent = JsonHelper.Serialize(new BusinessOpportunityLogEntity() { logContent = "test", logTitleId = 1, MobileType = 1, UserId = 1, UserIP = "127.0.0.1", DealerId = 50002218, logCreateTime = DateTime.Now, logTypeId = 1, ObjectId = 1 }), LogTableId = 1 }.ConvertToByteArray()), DateTime.Now));
                action(new DataParam(Encoding.UTF8.GetString(new ProduceDataForMQ() { LogContent = JsonHelper.Serialize(new BusinessOpportunityLogEntity() { logContent = "test", logTitleId = 1, MobileType = 1, UserId = 1, UserIP = "127.0.0.1", DealerId = 50002218, logCreateTime = DateTime.Now, logTypeId = 1, ObjectId = 1 }), LogTableId = 2 }.ConvertToByteArray()), DateTime.Now));
            }

        }
    }
    /// <summary>
    ///BusinessOpportunityLog数据实体
    /// </summary>
    [Serializable]
    public class BusinessOpportunityLogEntity
    {
        #region 变量定义
        ///<summary>
        ///
        ///</summary>
        private long _logId;
        ///<summary>
        ///
        ///</summary>
        private short _logTypeId;
        ///<summary>
        ///
        ///</summary>
        private int _dealerId;
        ///<summary>
        ///
        ///</summary>
        private int _userId;
        ///<summary>
        ///
        ///</summary>
        private string _userIP = String.Empty;
        ///<summary>
        ///
        ///</summary>
        private int _logTitleId;
        ///<summary>
        ///
        ///</summary>
        private string _logContent = String.Empty;
        ///<summary>
        ///
        ///</summary>
        private DateTime _logCreateTime;
        ///<summary>
        ///PC - 0 ,IPhone - 1,Android - 2, OhterPhone - 3
        ///</summary>
        private short _mobileType;
        ///<summary>
        ///
        ///</summary>
        private int _objectId;
        #endregion

        #region 构造函数

        ///<summary>
        ///
        ///</summary>
        public BusinessOpportunityLogEntity()
        {
        }
        ///<summary>
        ///
        ///</summary>
        public BusinessOpportunityLogEntity
        (
            long logId,
            short logTypeId,
            int dealerId,
            int userId,
            string userIP,
            int logTitleId,
            string logContent,
            DateTime logCreateTime,
            short mobileType,
            int objectId
        )
        {
            _logId = logId;
            _logTypeId = logTypeId;
            _dealerId = dealerId;
            _userId = userId;
            _userIP = userIP;
            _logTitleId = logTitleId;
            _logContent = logContent;
            _logCreateTime = logCreateTime;
            _mobileType = mobileType;
            _objectId = objectId;

        }
        #endregion

        #region 公共属性


        ///<summary>
        ///
        ///</summary>

        public long logId
        {
            get { return _logId; }
            set { _logId = value; }
        }

        ///<summary>
        ///
        ///</summary>

        public short logTypeId
        {
            get { return _logTypeId; }
            set { _logTypeId = value; }
        }

        ///<summary>
        ///
        ///</summary>

        public int DealerId
        {
            get { return _dealerId; }
            set { _dealerId = value; }
        }

        ///<summary>
        ///
        ///</summary>

        public int UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        ///<summary>
        ///
        ///</summary>

        public string UserIP
        {
            get { return _userIP; }
            set { _userIP = value; }
        }

        ///<summary>
        ///
        ///</summary>

        public int logTitleId
        {
            get { return _logTitleId; }
            set { _logTitleId = value; }
        }

        ///<summary>
        ///
        ///</summary>

        public string logContent
        {
            get { return _logContent; }
            set { _logContent = value; }
        }

        ///<summary>
        ///
        ///</summary>

        public DateTime logCreateTime
        {
            get { return _logCreateTime; }
            set { _logCreateTime = value; }
        }

        ///<summary>
        ///PC - 0 ,IPhone - 1,Android - 2, OhterPhone - 3
        ///</summary>

        public short MobileType
        {
            get { return _mobileType; }
            set { _mobileType = value; }
        }

        ///<summary>
        ///
        ///</summary>

        public int ObjectId
        {
            get { return _objectId; }
            set { _objectId = value; }
        }

        #endregion

    }
}
